package com.ruoyi.util;

import java.util.*;

public class StringUtil {

    //获取UUID
    public static String getUUID(){
        return UUID.randomUUID().toString().replaceAll("-","").toUpperCase();
    }

    /**
     * 处理主子表提交的数据,转为list
     *
     * @param arrayName  数组名称
     * @param attributes 属性集合
     * @param dataSource 数据源
     * @return
     */
    public static List<Map<String, Object>> MainSubTableToList(String arrayName, List<String> attributes, Map<String, String> dataSource) {
        int max = -1;
        for (Map.Entry entry : dataSource.entrySet()) {
            System.out.println(entry.getKey() + "=" + entry.getValue());
            String key = entry.getKey().toString();
            if (key.indexOf("[") != -1 && key.indexOf("]") != -1 && key.indexOf(".") != -1 && key.indexOf(arrayName) != -1) {
                int start = key.indexOf("[");
                int end = key.indexOf("]");
                int num = Integer.valueOf(key.substring(start + 1, end));
                if(num > max){
                    max = num;
                }
            }
        }
        List<Map<String, Object>> dataList = new ArrayList<>();
        for (int i = 0; i <= max; i++) {
            Map map = new HashMap();
            for (String attribute : attributes) {
                map.put(attribute, dataSource.get(arrayName + "[" + i + "]." + attribute));
            }
            dataList.add(map);
        }
        return dataList;
    }
}
